<?php
namespace app\index\controller;
use \think\Controller;
use \think\View;
use app\index\model\TodayInHistory;
use app\index\model\Weather;
class Index extends Controller
{
    public function index()
    {
      if(!isset($_GET['Content'])){
        return "请输入您的内容！";exit;
      }
    	$keywords = $_GET['Content'];
        $url = "http://api.qingyunke.com/api.php?key=free&appid=0&msg='".$keywords."'";
        $return_data = file_get_contents($url);
        //$data = json_decode($return_data);
        //echo $data['content'];exit;
        $explode_data = explode(',"content":"', $return_data);
       $data = substr($explode_data[1],0,strlen($explode_data[1])-2);
        return $data;exit;
        var_dump($return_data);exit;
        $this->assign("data",$data);
        return $this->fetch('index');
    }

    //历史上的今天
     public function todayInHistory()
    {
        $todayInHistory = new TodayInHistory();
        $res = $todayInHistory->todayInHistory();
        $data = "";
        for ($i=0; $i < count($res) ; $i++) { 
          $data = $data.$res[$i]->year.'-'.$res[$i]->month.'-'.$res[$i]->day.'：'.$res[$i]->title.'<br>';
        }
        echo $data;
        //echo count($res);
        //  foreach ($res as $key => $v) {
        //   $data = $data.$v['title'].'<br>';
        // }
        // var_dump($data);
    }

     //天气
     public function weather()
    {
        $weather = new Weather();
        $res = $weather->weather();
        var_dump($res);
    }

    public function valid_access_token()
    {
    	$signature = $_GET['signature'];
    	$timestamp = $_GET['timestamp'];
    	$nonce = $_GET['nonce'];
    	$echostr = $_GET['echostr'];
    	$token = 'lushaoming5201314';
  //   	try{
  //   	$array = array($token,$timestamp, $nonce);
  //   	sort($array, SORT_STRING);
  //   	$str = implode($array);
  //   	$sha1_str = sha1($str);
  //   	} catch (Exception $e) {
		// 	//print $e . "\n";
		// 	//return array(ErrorCode::$ComputeSignatureError, null);
		// }
		// if($signature==$sha1_str){//验证成功，原样返回$echostr
		// 	return $echostr;

		// }else{
		// 	echo "fialure";
		// }
  //       //echo 'lushao5201314';

      //排序
    try {
      $array = array($token, $timestamp, $nonce);
      sort($array, SORT_STRING);
      $str = implode($array);
      $test = sha1($str);
      if($test==$signature){
        return $echostr;
      }else{
        return 0;
      }
     
    } catch (Exception $e) {
      //print $e . "\n";
      return array(-40001, null);
    }
    }

  

    

        
   


}
